﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using fideeraSoft.DAO;
using System.Data.SqlClient;
using fideeraSoft.Utils;
using fideeraSoft.Entities;

namespace fideeraSoft.DAOimpl
{
    public class SalidaDAOSQLimpl:SalidaDAO
    {

        #region BUSCAR NUEVO ULTIMO ID REMITO SALIDA
        public int buscarNuevoUltimoIdRemitoSalidaEnBD()
        {
            int ID = 0;
            SqlConnection conn = SQLclass.conectarSQL();
            try
            {
                conn.Open();

                string query = "SELECT TOP 1 ID FROM RemitoSalida ORDER BY ID DESC ";
                SqlCommand cmd = new SqlCommand(query, conn);

                SQLclass.commandQuery(conn, cmd, query);
                SqlDataReader reader = cmd.ExecuteReader();

                if (reader.HasRows)
                {

                    while (reader.Read())
                    {
                        ID = reader.GetInt32(0);
                        //  CLogger.Append("[buscarNuevoUltimoIdProveedorEnBD] ID : " + ID, CLogger.ERROR);      //DEBUG
                    }
                    ID++;
                }
                else
                {
                    ID = 1;
                }
            }
            catch (Exception e)
            {
                CLogger.Append("[buscarNuevoUltimoIdRemitoSalidaEnBD] Exception : " + e, CLogger.ERROR);      //DEBUG
                throw new MiException("[buscarNuevoUltimoIdRemitoSalidaEnBD] Exception: " + e);
            }
            finally
            {
                conn.Close();
            }
            // CLogger.Append("[buscarNuevoUltimoIdProveedorEnBD] RETURN ID : " + ID, CLogger.ERROR);      //DEBUG
            return ID;

        }
        #endregion

        #region ALTA Remito Salida
        public void altaRemitoSalida(RemitoSalida remitoSalida)
        {
            SqlConnection conn = null;
            try
            {
                conn = SQLclass.conectarSQL();
                conn.Open();

                string query = "SET IDENTITY_INSERT RemitoSalida ON; INSERT INTO RemitoSalida (ID,codigo,fechaRemitoSalida,usuario) VALUES (@IdRemitoSalida,@Codigo,@FechaRemitoSalida,@IdUsuario)";
                //string query = "INSERT INTO Proveedor (razonSocial,responsable,cuil,direccion,localidad,provincia,telefono,celular) VALUES (@RazonSocial,@Responsable,@Cuil,@Direccion,@Localidad,@Provincia,@Telefono,@Celular)";
                SqlCommand cmd = new SqlCommand(query, conn);

                cmd.Parameters.AddWithValue("@IdRemitoSalida", remitoSalida.IdSalida);
                cmd.Parameters.AddWithValue("@Codigo", remitoSalida.Codigo);
                cmd.Parameters.AddWithValue("@FechaRemitoSalida", remitoSalida.FechaRemitoSalida);

                cmd.Parameters.AddWithValue("@IdUsuario", remitoSalida.Usuario.IdUsuario);
                //
                //cmd.Parameters.AddWithValue("@IdProduccion", remitoSalida.Produccion);
                //cmd.Parameters.AddWithValue("@CantidadPaquetes", remitoSalida.CantidadPaquetes);
                

                cmd.ExecuteNonQuery();
                //cmd.Parameters.Clear();
                // cmd.CommandText = "SELECT @@IDENTITY";
                // int insertID=Convert.ToInt32(cmd.ExecuteScalar());

                //  cmd.Dispose();
                cmd = null;


            }
            catch (Exception e)
            {
                CLogger.Append("[altaRemitoSalida] Exception alta: " + e, CLogger.ERROR);      //DEBUG
                throw new MiException("[altaRemitoSalida] Exception alta: " + e);
            }
            finally
            {
                conn.Close();
            }

        }

        #endregion


        #region ELIMINAR REMITOs SALIDA POR ID
        public void eliminarRemitoSalidaPorId(int id)
        {
            SqlConnection conn = SQLclass.conectarSQL();
            try
            {
                conn.Open();

                string query = "DELETE FROM RemitoSalida WHERE ID='"+id.ToString()+"'";
                SqlCommand cmd = new SqlCommand(query, conn);
                cmd.ExecuteNonQuery();

            }
            catch (Exception e)
            {
                CLogger.Append("[eliminarRemitoSalidaPorId] Exception : " + e, CLogger.ERROR);      //DEBUG
                throw new MiException("[eliminarRemitoSalidaPorId] Exception: " + e);
            }
            finally
            {
                conn.Close();
            }
        }

        #endregion

        #region ELIMINAR Todos LOS REMITOs SALIDA EN DB
        public void eliminarTodosLosRemitosSalidaEnDb()
        {
            SqlConnection conn = SQLclass.conectarSQL();
            try
            {
                conn.Open();

                string query = "DELETE FROM RemitoSalida";
                SqlCommand cmd = new SqlCommand(query, conn);
                cmd.ExecuteNonQuery();

            }
            catch (Exception e)
            {
                CLogger.Append("[eliminarTodosLosRemitosSalidaEnDb] Exception : " + e, CLogger.ERROR);      //DEBUG
                throw new MiException("[eliminarTodosLosRemitosSalidaEnDb] Exception: " + e);
            }
            finally
            {
                conn.Close();
            }
        }

        #endregion
    }
}
